/**
 * gwt-validator 
 *
 * Copyright (C) 2006  Nikolaus Rumm, Christian Simonutti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package gwt.validator.client;

import gwt.validator.client.action.Action;
import gwt.validator.client.check.Check;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * A <code>ValidationScope</code> is a container for checks (validation rules)
 * and actions.
 * 
 * All issues that are raised by all checks within a validation scope will be
 * propagated to all actions within that scope.
 * 
 * @author <a href="mailto:nikolaus.rumm@gmail.com">Nikolaus Rumm</a>
 */
public class ValidationScope {

	/**
	 * The child scopes
	 */
	private List childScopes;

	/**
	 * The validation rules that are used by this scope
	 */
	private List checks;

	/**
	 * The (local) actions that are used by this scope
	 */
	private List actions;

	/**
	 * The global actions that are used by this scope.
	 */
	private List globalActions;

	/**
	 * Constructor
	 */
	public ValidationScope() {
		super();
		this.childScopes = new ArrayList();
		this.checks = new ArrayList();
		this.actions = new ArrayList();
		this.globalActions = new ArrayList();
	}

	/**
	 * Constructor.
	 * 
	 * @param aChecks
	 *            The checks
	 * @param aActions
	 *            The actions
	 */
	public ValidationScope(Check[] aChecks, Action[] aActions) {
		this();
		if (aChecks != null) {
			this.checks = Arrays.asList(aChecks);
		}
		if (aActions != null) {
			this.actions = Arrays.asList(aActions);
		}
	}

	/**
	 * Constructor.
	 * 
	 * @param aChildScopes
	 *            The child validation scopes
	 * @param aChecks
	 *            The checks
	 * @param aActions
	 *            The actions
	 */
	public ValidationScope(ValidationScope[] aChildScopes, Check[] aChecks,
			Action[] aActions) {
		this();
		if (aChildScopes != null) {
			this.childScopes = Arrays.asList(aChildScopes);
		}
		if (aChecks != null) {
			this.checks = Arrays.asList(aChecks);
		}
		if (aActions != null) {
			this.actions = Arrays.asList(aActions);
		}
		this.globalActions = new ArrayList();
	}

	/**
	 * Constructor.
	 * 
	 * @param aChecks
	 *            The checks
	 * @param aActions
	 *            The actions
	 * @param aGlobalActions
	 *            The global actions
	 */
	public ValidationScope(Check[] aChecks, Action[] aActions,
			Action[] aGlobalActions) {
		this();
		if (aChecks != null) {
			this.checks = Arrays.asList(aChecks);
		}
		if (aActions != null) {
			this.actions = Arrays.asList(aActions);
		}
		if (aGlobalActions != null) {
			this.globalActions = Arrays.asList(aGlobalActions);
		}
	}

	/**
	 * Constructor.
	 * 
	 * @param aChildScopes
	 *            The child validation scopes
	 * @param aChecks
	 *            The checks
	 * @param aActions
	 *            The actions
	 * @param aGlobalActions
	 *            The global actions
	 */
	public ValidationScope(ValidationScope[] aChildScopes, Check[] aChecks,
			Action[] aActions, Action[] aGlobalActions) {
		this();
		if (aChildScopes != null) {
			this.childScopes = Arrays.asList(aChildScopes);
		}
		if (aChecks != null) {
			this.checks = Arrays.asList(aChecks);
		}
		if (aActions != null) {
			this.actions = Arrays.asList(aActions);
		}
		if (aGlobalActions != null) {
			this.globalActions = Arrays.asList(aGlobalActions);
		}
	}

	/**
	 * Adds the give child validation scope to this scope.
	 * 
	 * @param aChildScope
	 *            The child validation scope to add
	 */
	public void addChildValidationScope(ValidationScope aValidationScope) {
		this.childScopes.add(aValidationScope);
	}

	/**
	 * Adds the given check to the scope.
	 * 
	 * @param aCheck
	 *            The check to add
	 */
	public void addCheck(Check aCheck) {
		this.checks.add(aCheck);
	}

	/**
	 * Adds the given action to the scope.
	 * 
	 * @param aAction
	 *            The action
	 */
	public void addAction(Action aAction) {
		this.actions.add(aAction);
	}

	/**
	 * Adds the given global action to the scope.
	 * 
	 * @param aAction
	 *            The action
	 */
	public void addGlobalAction(Action aAction) {
		this.globalActions.add(aAction);
	}

	/**
	 * Read-accessor to the scope's child validation scopes.
	 * 
	 * @return The child validation scopes
	 */
	public List getChildValidationScopes() {
		return this.childScopes;
	}

	/**
	 * Read-accessor to the scope's checks.
	 * 
	 * @return The checks
	 */
	public List getChecks() {
		return this.checks;
	}

	/**
	 * Read-accessor to the scope's actions.
	 * 
	 * @return The actions
	 */
	public List getActions() {
		return this.actions;
	}

	/**
	 * Read-accessor to the scope's global actions.
	 * 
	 * @return The global actions
	 */
	public List getGlobalActions() {
		return this.globalActions;
	}
}
